package com.yzt.message.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Keep;

import com.yzt.message.R;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.utils.SpannableFactory;

import java.util.List;

import io.rong.imkit.conversation.extension.component.emoticon.AndroidEmoji;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imlib.model.MessageContent;

/**
 * 系统自定义消息
 */
@Keep
public class RXGroupJoinMessageItemProvider extends CustomBaseMessageItemProvider<RXGroupJoinContent> {

    public RXGroupJoinMessageItemProvider() {
        mConfig.showReadState = false;
        mConfig.showPortrait = false;
        mConfig.showContentBubble = false;
        mConfig.centerInHorizontal = true;
    }

    @Override
    protected io.rong.imkit.widget.adapter.ViewHolder onCreateMessageContentViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.nomi_rc_item_create_group_tip_text_message, viewGroup, false);
        return new ViewHolder(view.getContext(), view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void bindMessageContentViewHolder(io.rong.imkit.widget.adapter.ViewHolder viewHolder, io.rong.imkit.widget.adapter.ViewHolder viewHolder1,
                                                RXGroupJoinContent content, UiMessage data, int i, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {
        ViewHolder holder = (ViewHolder) viewHolder;
        StringBuilder sb = new StringBuilder();

        String userName = "";

        if (content.getBeInvitedUsers() != null) {
            for (String name : content.getBeInvitedUsers()) {
                sb.append(name).append("、");
            }
            userName = sb.substring(0, sb.length() - 1);
        }

        if (RXingUtils.getPersonDetail().getUserBasic().getUserId().equals(content.getInvitedUserId())) {
            holder.message.setText(SpannableFactory.create("你邀请了")
                    .append(userName)
                    .foregroundResColor(R.color.color_FFD08E)
                    .append("加入群聊").build());
        } else {
            holder.message.setText(SpannableFactory.create(content.getInvitedUserName() + "邀请了")
                    .append(userName)
                    .foregroundResColor(R.color.color_FFD08E)
                    .append("加入群聊").build());
        }
    }

    @Override
    protected boolean onItemClick(io.rong.imkit.widget.adapter.ViewHolder viewHolder, RXGroupJoinContent RXGroupJoinContent, UiMessage uiMessage, int i, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {
        return false;
    }

    @Override
    protected boolean isMessageViewType(MessageContent messageContent) {
        return messageContent instanceof RXGroupJoinContent && !messageContent.isDestruct();
    }

    @Override
    public Spannable getSummarySpannable(Context context, RXGroupJoinContent data) {
        if (data == null) {
            return null;
        }
        String content = data.getContent();
        if (content != null) {
            if (content.length() > 100) {
                content = content.substring(0, 100);
            }
            return new SpannableString(AndroidEmoji.ensure(content));
        }

        return null;
    }

    private static class ViewHolder extends io.rong.imkit.widget.adapter.ViewHolder {

        TextView message;

        ImageView ivTipBell;

        public ViewHolder(Context context, View view) {
            super(context, view);

            this.message = view.findViewById(R.id.tv_content);
            this.ivTipBell = view.findViewById(R.id.iv_tip_bell);
        }
    }
}

